﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Common.HttpApis
{    
    public class ChatGptHttp
    {
        
        public string prompt { get; set; }
        public string userId { get; set; }
        public bool network { get; set; } = true;
        public string system { get; set; } = "";
        public bool withoutContext { get; set; }
        public bool stream { get; set; } 



        public static string Post(string postData)
        {
            string strHeaders = "accept-encoding: gzip, deflate, br\r\naccept-language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6\r\norigin: https://chat2.jinshutuan.com\r\nsec-ch-ua: \"Microsoft Edge\";v=\"113\", \"Chromium\";v=\"113\", \"Not-A.Brand\";v=\"24\"\r\nsec-ch-ua-mobile: ?0\r\nsec-ch-ua-platform: \"Windows\"\r\nsec-fetch-dest: empty\r\nsec-fetch-mode: cors\r\nsec-fetch-site: cross-site";
            //string strHeaders = ":authority: api.binjie.fun\r\n:method: POST\r\n:path: /api/generateStream\r\n:scheme: https\r\naccept: application/json, text/plain, */*\r\naccept-encoding: gzip, deflate, br\r\naccept-language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6\r\ncontent-length: 309\r\ncontent-type: application/json\r\norigin: https://chat2.jinshutuan.com\r\nreferer: https://chat2.jinshutuan.com/\r\nsec-ch-ua: \"Microsoft Edge\";v=\"113\", \"Chromium\";v=\"113\", \"Not-A.Brand\";v=\"24\"\r\nsec-ch-ua-mobile: ?0\r\nsec-ch-ua-platform: \"Windows\"\r\nsec-fetch-dest: empty\r\nsec-fetch-mode: cors\r\nsec-fetch-site: cross-site";

            string url = "https://api.binjie.fun/api/generateStream"; // 获得数据
            //postData = "{\"username\":\"your_username\",\"password\":\"your_password\"}"; // 请求体内容，JSON格式
            string userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50"; // 设置User-Agent

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Timeout = 3 * 60 * 1000;
            request.Method = "POST";
            request.UserAgent = userAgent;
            request.Accept= "application/json, text/plain, */*";
            request.Referer = "https://chat2.jinshutuan.com/";
            request.ContentType = "application/json";
            //request.ContentLength = postData.Length;
            //string[] headers=strHeaders.Replace("\n","").Split('\r');
            string[] headers = strHeaders.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in headers)
            {
                string[] parts = line.Split(new string[] { ": " }, StringSplitOptions.RemoveEmptyEntries);
                request.Headers.Add(parts[0], parts[1]);
            }
            
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(postData);
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string result = string.Empty;

            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }

            //Console.WriteLine(result); // 打印登录结果

            response.Close();
            return result;
        }




        public static void Post(string postData,Action<int> outCharFunc)
        {
            string strHeaders = "accept-encoding: gzip, deflate, br\r\naccept-language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6\r\norigin: https://chat2.jinshutuan.com\r\nsec-ch-ua: \"Microsoft Edge\";v=\"113\", \"Chromium\";v=\"113\", \"Not-A.Brand\";v=\"24\"\r\nsec-ch-ua-mobile: ?0\r\nsec-ch-ua-platform: \"Windows\"\r\nsec-fetch-dest: empty\r\nsec-fetch-mode: cors\r\nsec-fetch-site: cross-site";
            //string strHeaders = ":authority: api.binjie.fun\r\n:method: POST\r\n:path: /api/generateStream\r\n:scheme: https\r\naccept: application/json, text/plain, */*\r\naccept-encoding: gzip, deflate, br\r\naccept-language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6\r\ncontent-length: 309\r\ncontent-type: application/json\r\norigin: https://chat2.jinshutuan.com\r\nreferer: https://chat2.jinshutuan.com/\r\nsec-ch-ua: \"Microsoft Edge\";v=\"113\", \"Chromium\";v=\"113\", \"Not-A.Brand\";v=\"24\"\r\nsec-ch-ua-mobile: ?0\r\nsec-ch-ua-platform: \"Windows\"\r\nsec-fetch-dest: empty\r\nsec-fetch-mode: cors\r\nsec-fetch-site: cross-site";

            string url = "https://api.binjie.fun/api/generateStream"; // 获得数据
            //postData = "{\"username\":\"your_username\",\"password\":\"your_password\"}"; // 请求体内容，JSON格式
            string userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50"; // 设置User-Agent

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Timeout = 3 * 60 * 1000;
            request.Method = "POST";
            request.UserAgent = userAgent;
            request.Accept = "application/json, text/plain, */*";
            request.Referer = "https://chat2.jinshutuan.com/";
            request.ContentType = "application/json";
            //request.ContentLength = postData.Length;
            //string[] headers=strHeaders.Replace("\n","").Split('\r');
            string[] headers = strHeaders.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in headers)
            {
                string[] parts = line.Split(new string[] { ": " }, StringSplitOptions.RemoveEmptyEntries);
                request.Headers.Add(parts[0], parts[1]);
            }

            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(postData);
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string result = string.Empty;

            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                //result = reader.ReadToEnd();
                //StringBuilder responseBuilder = new StringBuilder();
                while (true)
                {
                    int character = reader.Read();
                    outCharFunc(character);
                    if (character == -1)
                    {
                        break;
                    }
                    
                    //responseBuilder.Append((char)character);
                }

            }

            //Console.WriteLine(result); // 打印登录结果

            response.Close();
        }


        static async Task Main(string[] args)
        {
            // 请求地址和请求体数据
            string url = "https://example.com/login";
            var requestData = new
            {
                username = "your-username",
                password = "your-password"
            };
            string requestBody = JsonConvert.SerializeObject(requestData);

            HttpClient httpClient = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);

            // 设置请求头
            request.Headers.Add("User-Agent", "Mozilla/5.0");
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // 设置请求体
            StringContent content = new StringContent(requestBody, Encoding.UTF8, "application/json");
            request.Content = content;

            // 发送请求
            HttpResponseMessage response = await httpClient.SendAsync(request);

            // 读取返回的字符串
            string responseString = await response.Content.ReadAsStringAsync();

            // 逐个字符输出返回的字符串
            foreach (char c in responseString)
            {
                Console.Write(c);
            }
        }

    }
}




